<?php
session_start();

include_once("../../Misc.php");
include_once("../../helpers/CHtml.php");
include_once("../../helpers/CJavascript.php");
include_once("../../helpers/Composer.php");
include_once("../../classCol/classOracleDb.php");
include_once("../../models/GlobalFetchers.php");
include_once("../../models/GlobalHandlers.php");

include_once("../controllers/local.php");
include_once("../models/fetchers.php");
include_once("../models/handlers.php");

$type       = _CASH_BANK_IN;
$tx         = _CASH_BANK_INVOICE_NAME;

$GHtml      = new CGHtml;
$GJs        = new CGJavascript;
$Misc       = new Misc;

$CLocal     = new CLocal;
$CLHandlers = new CLHandlers;
$CLFetchers = new CLFetchers;
$CFetchers  = new GlobalFetchers;
$CHandlers  = new GlobalHandlers;

$SourceDb   = new Composer;
$SourceDb   = $SourceDb->init("db");

$SourceDb->add("srcDbMstModel",           "MST_MODEL");
$SourceDb->add("srcDbMstComposition",     "MST_MODEL_COMPOS");
$SourceDb->add("srcDbDtlSN",              "MUT_WHS_DTL");
$SourceDb->add("srcDbSubDtlSN",           "MUT_WHS_SUB_DTL");
$SourceDb->add("srcDbTmpDtlSN",           "MUT_WHS_DTL_TMP");
$SourceDb->add("srcDbTmpSubDtlSN",        "MUT_WHS_SUB_DTL_TMP");
$SourceDb->add("srcDbDtlComposSN",        "MUT_WHS_COMPOS_DTL");
$SourceDb->add("srcDbSubDtlComposSN",     "MUT_WHS_COMPOS_SUB_DTL");
$SourceDb->add("srcDbTmpDtlComposSN",     "MUT_WHS_COMPOS_DTL_TMP");
$SourceDb->add("srcDbTmpSubDtlComposSN",  "MUT_WHS_COMPOS_SUB_DTL_TMP");
$SourceDb->add("srcDbSerialNumber",       "SN_SUB_DTL");

$Db = new oracleDb;
$Db->init($_SESSION["ORCL"],
          $_SESSION["OPASS"],
          $_SESSION["SID"],
          _NO_COMMENT);

if($Db->connect()){


}else{

  $Misc->usrDieJs(_DB_CONN_ERROR);
}

echo  $GHtml->title("Serial Number " .$_GET["modelId"]).
      $GJs->includeThis(_TYPE_CSS,"/ecs/myMenu.css").
      $GJs->includeThis(_TYPE_JS ,"/ecs/javascript/GMisc.js").
      $GJs->includeThis(_TYPE_JS ,"/ecs/javascript/GAjax.js").
      $GJs->includeThis(_TYPE_JS ,"/ecs/tocmenu_611/javascript/misc.js").
      $GJs->includeThis(_TYPE_JS ,"/ecs/tocmenu_611/javascript/ajax.js").
      $GJs->stopRKey();

/**
 * Handle the process
 */
if($_POST["Save"]){

  require("../controllers/procSave.php");
}else
if($_POST["Add"]){

  require("../controllers/procAdd.php");
}else
if($_POST["Drop"]){

  require("../controllers/procDrop.php");
}else{

  /**
   * make sure we are clean
   */
  unset($_SESSION["init_sub"]);

  /**
   * @todo reevaluate if these steps neccesary
   *
   * Since we could use another menu to reduce
   * these steps
   * check if this model is composited
   */
  $CFetchers->isModelComposited($Db,
                                $_GET["modelId"],
                                $SourceDb->getValue("srcDbMstComposition"),
                                $nComposited,
                                $lstComposited);

  if($nComposited > 0){
  //if($nComposited == _FRESH_ENTRY){

    /**
     * @todo check if model type is production then skip
     */

    /**
     * fetch foreach composite
     * if it is SN-ed
     */
    for($iCom = 0; $iCom < $nComposited ; $iCom++){

      $CFetchers->isModelSerNumber($Db,
                                   $lstComposited[$iCom],
                                   $SourceDb->getValue("srcDbMstModel"),
                                   $isSerNumber);

      $CFetchers->qtyOfComposition($Db,
                                   $_GET["modelId"],
                                   $lstComposited[$iCom],
                                   $SourceDb->getValue("srcDbMstComposition"),
                                   $qtyOfComposition);

      /**
       * qtyOfComposition has to be multiplied
       * with srcQty
       * eg. A is 2B then
       *     10A is 2B * 10 => 20B
       */
      $qtyOfComposition *= $_GET["srcQty"];

      /**
       * open new child Window to handle this tx
       * we are done
       */
      echo  $GJs->jsScript(NULL, "Misc.txWindowCompos(\"".$_GET["model"]."\",
                                                      \"".$_GET["modelId"]."\",
                                                      \"".$lstComposited[$iCom]."\",
                                                      \"".$qtyOfComposition."\",
                                                      \"".$_GET["tmpJid"]."\",
                                                      \"".$_GET["srcWhs"]."\",
                                                      \"".$_SERVER["HTTP_HOST"]."\");");
    }

  }else{

    /* do nothing */
  }

  /**
   * Fetch previous entried
   * if any
   */
  $CLFetchers->subDtlSerNumber($Db,
                               $_SESSION["tmpJid"],
                               $SourceDb->getValue("srcDbTmpSubDtlSN"),
                               $nData,
                               $lstData);

  if($nData > 0){

    for($iRow = 0; $iRow < $nData ; $iRow++){

      $_POST["snEnt"][$iRow]   = $lstData["SN"][$iRow];
    }
  }else{

    $nData = 4;
  }

  $maxRow = $nData;
}

/**
 * @todo check if this parent is rakitan
 *
 * YES: Close this dialog
 * NO : Please Entry
 */

/**
 * i want to try to use PHP+HTML combination
 * so i could have one form as template
 */

?>

<form name="modelSN" action=# method=post>
<label> <?=$_GET["model"] ?> </label>
<table border=1 class="dedeq">
<tr>
<th> No   </th>
<th> Drop </th>
<th> SN   </th>
</tr>

<?for($iRow = 0; $iRow < $_GET["srcQty"]; $iRow++): ?>

  <tr>
  <td> <?=($iRow + 1) ?> </td>

  <td> <?= $GHtml->formCheckBox("dropBox[]",
                                ($iRow + 1),
                                $Misc->stdProperties())  ?> </td>

  <td> <?= $GHtml->formInput("snEnt[".$iRow."]",
                             $_POST["snEnt"][$iRow],
                             $Misc->stdProperties().
                             $Misc->stdKeyProp("snEnt[".($iRow+1)."]")); ?> </td>
  </tr>
<?endfor; ?>

</table><br/>

<?=$GHtml->formInputButton("Save",
                           "Save",
                           "Save",
                           $stlButton) ?>

<?/*=$GHtml->formInputButton("Add",
                           "Add",
                           "Add",
                           $stlButton)*/ ?>

<?/*=$GHtml->formInputButton("Drop",
                           "Drop",
                           "Drop",
                           $stlButton)*/ ?>

</form>